#include "SyncAlg.hpp"
#include <cstring>
#include <spdlog/spdlog.h>

SyncAlg::SyncAlg(int cameraNumber)
  : cameraNum(cameraNumber)
{
  ImageList.resize(cameraNumber);
}

SyncAlg::~SyncAlg()
{

}

void SyncAlg::setResultCallback(std::function<void(const std::vector<ImageType>&)> func)
{
  funcResult=func;
}

bool SyncAlg::InputImage(ImageType& image,int camera_index)
{
  //spdlog::info("Current ID {},syncCount {},syncFrameID {}",image.FrameID,syncCount.load(),syncFrameID.load());
  if(image.FrameID==syncFrameID)
  {
    memcpy(&ImageList[syncCount],&image,sizeof(ImageType));
    syncCount++;
    if(syncCount==cameraNum)
    {
      funcResult(ImageList);
      for(auto & i : ImageList)
        delete [] i.data;
      syncCount=0;
    }
  }
  else if(image.FrameID<syncFrameID)
  {
    delete []  image.data;
    return false;
  }
  else
  {
    for(int i=0;i<syncCount;i++)
      delete [] ImageList[i].data;
    syncCount=1;
    syncFrameID=image.FrameID;
    memcpy(&ImageList[0],&image,sizeof(ImageType));
  }
  return true;
}
